package com.musiklegal;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.telephony.TelephonyManager;
public class Settings extends Activity{
	
	String balance;
	TextView baln;
	TextView uid;
	TextView usr;
	TextView fb,tw;
	ImageView pp;
	 Handler h = new Handler();
	String stat;
	static boolean lang = false;
	//title
	static String jhentisubs;
	static String jmulaisubs;
	static String jbangkrut;
	static String jteman;
	static String joption;
	
	//content
	static String mulaisubs;
	static String mulaisubs2;
    static String hentisubs;
    static String bangkrut;
    static String gakonek;
    static String error;
    static String thank;
    static String sd;
    static String expp;
    static String expd;
    static String del;
    static String deal;
    static String playlist;
    static String info;
    static String nosos;
    static String def;
    static String cari;
    static String nodef;
    static String help;
    //itombol
    static String yes;
    static String no;
    static String topup;
    static String logout;
    
    Calendar ci = Calendar.getInstance();
	 String tid =""+ci.get(Calendar.YEAR) + "-" + 
   (ci.get(Calendar.MONTH) + 1) + "-" +
   ci.get(Calendar.DAY_OF_MONTH) + " " +
   ci.get(Calendar.HOUR) + ":" +
   ci.get(Calendar.MINUTE) +  ":" +
   ci.get(Calendar.SECOND);
    
    public void onResume()
    {
    	super.onResume();
    	if(!MusikLegal.internet(this))
   	 	{
   		 
   		 finish();
   	 	}
    	check();
    	
    }
    
    public boolean onPrepareOptionsMenu(Menu menu)
	{

    	menu.clear();
		menu.add(0,MainMenu.TopUp,0,"SMS Subscribe");
		
		menu.add(0,MainMenu.Subs,0,MainMenu.subsc);
		if(MusikLegal.mPreferences.getString("TWId", null)!=null)
			menu.add(0,MainMenu.Logout,0,"Disconnect Twitter");
		if(MusikLegal.mPreferences.getString("FBId", null)!=null)
			menu.add(0,MainMenu.DiscFB,0,"Disconnect FB");
		
		
		return true;
	}
	
    Button fbcon;
    Button twcon;
    Button langu;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.settings);
        MainMenu.setStatus(this);
        fbcon = (Button) findViewById(R.id.fbcon);
        twcon = (Button) findViewById(R.id.twcon);
        langu = (Button) findViewById(R.id.lang);
        fb = (TextView) findViewById(R.id.Fbname);
        tw = (TextView) findViewById(R.id.TWname);
        pp = (ImageView) findViewById(R.id.header);
        fbcon.setVisibility(View.INVISIBLE);
        twcon.setVisibility(View.INVISIBLE);
        if(MusikLegal.mPreferences.getString("Lang", "Indo").equalsIgnoreCase("Indonesia"))
		langu.setText("Ganti Bahasa");
        else 
        langu.setText("Change Language");
        
        check();
    	
       
        
       
        
        
    	if (MusikLegal.mPreferences.getString("SubID", "-").equals(null))
    			stat = "Tidak Berlangganan";
    	else
    		 stat = "Berlangganan";
    		
		
      
        final String username = MusikLegal.mPreferences.getString("UserName", "NoUser");
        final String password = MusikLegal.mPreferences.getString("PassWord", "NoPass");
        new Thread(new Runnable(){

			public void run() {
				if(MusikLegal.internet(getApplicationContext()))
		        {
		        try{
		        	DefaultHttpClient client = new DefaultHttpClient();
			        HttpPost bal = new HttpPost("http://secure.musiklegal.com/user/balance");
			        List< BasicNameValuePair > nvbs = new ArrayList< BasicNameValuePair >();
			        nvbs.add(new BasicNameValuePair("USERNAME", username));
			        nvbs.add(new BasicNameValuePair("PASSWORD", password));	
		        	 UrlEncodedFormEntity b_entity = new UrlEncodedFormEntity(nvbs,HTTP.UTF_8);
		        	 bal.setEntity(b_entity);
		        	 HttpResponse response4 = client.execute(bal);
		        	 HttpEntity responseEntity4 = response4.getEntity();
		        	 
		        	 SAXParserFactory lpf = SAXParserFactory.newInstance();
		             SAXParser lp = lpf.newSAXParser();
		             XMLReader lr = lp.getXMLReader();
		             
		             LoginHandler balHand = new LoginHandler();
		             lr.setContentHandler(balHand);
		             lr.parse(retrieveInputStream(responseEntity4));
		             ParsedLoginDataSet balset = balHand.getParsedLoginData();
		             
		             SharedPreferences.Editor editor=MusikLegal.mPreferences.edit();
		             editor.putString("Balance", balset.getBal());
		             editor.commit();
		        }
		        catch (Exception e)
		        {
		        	
		            
		        }
		        }
				
			}}).start();
        
        
        
        
        
        
         usr = (TextView) findViewById(R.id.usern);
		 usr.setText("Username           :"+MusikLegal.mPreferences.getString("UserName", "NoUser"));
         uid = (TextView) findViewById(R.id.uid);
		 uid.setText("UID              	    :"+MusikLegal.getUID(this));
		 baln = (TextView) findViewById(R.id.bal);
		 baln.setText("Voucher		  	    :"+MusikLegal.mPreferences.getString("Balance", "0"));
		 
		 
		 
		 
		 fbcon.setOnClickListener(new OnClickListener(){

				public void onClick(View arg0) {
					if(MusikLegal.internet(getApplicationContext()))
					{
						String uname = MusikLegal.mPreferences.getString("UserName", "");
						String pass = MusikLegal.mPreferences.getString("PassWord", "");
						LoginWeb.fromSettings=true;
						LoginWeb.urlTo="http://www.musiklegal.com/fbauth.php?source=mobile&touch=1&username="+uname+"&password="+pass;
						startActivity(new Intent(getApplicationContext(),LoginWeb.class));
						LoginWeb.p = Settings.this;
					}
					else
     				Toast.makeText(getApplicationContext(), Settings.gakonek, Toast.LENGTH_SHORT).show();
			
					
				}});
           twcon.setOnClickListener(new OnClickListener(){

				public void onClick(View arg0) {
					if(MusikLegal.internet(getApplicationContext()))
					{
						String uname = MusikLegal.mPreferences.getString("UserName", "");
						String pass = MusikLegal.mPreferences.getString("PassWord", "");
						LoginWeb.fromSettings=true;
						LoginWeb.urlTo="http://www.musiklegal.com/twauth.php&username="+uname+"&password="+pass;
						startActivity(new Intent(getApplicationContext(),LoginWeb.class));
						LoginWeb.p = Settings.this;
					}
					else
       				Toast.makeText(getApplicationContext(), Settings.gakonek, Toast.LENGTH_SHORT).show();
			
					
				}});
           
           langu.setOnClickListener(new OnClickListener(){

				public void onClick(View arg0) {
					if(MusikLegal.internet(getApplicationContext()))
					{
						if(!lang)
						{
							SharedPreferences.Editor editor=MusikLegal.mPreferences.edit();
							editor.putString("Lang", "English");
							editor.commit();
						lang=true;
						jhentisubs=Language.thentisubs;
						jmulaisubs=Language.tmulaisubs;
						jbangkrut=Language.tbangkrut;
						jteman=Language.tfriend;
						joption=Language.toption;

						mulaisubs=Language.emulaisubs;
						mulaisubs2=Language.emulaisubs2;
					    hentisubs=Language.ask;
					    bangkrut=Language.ebangkrut;
					    gakonek=Language.egakonek;
					    error=Language.eerror;
					    thank=Language.ethanks;
					    sd=Language.esd;
					    expp=Language.eexpp;
					    expd=Language.eexpd;
					    del=Language.edel;
					    deal=Language.edeal;
					    playlist=Language.eplaylist;
					    info=Language.einfo;
					    nosos=Language.enosos;
					    def=Language.edef;
					    cari=Language.ecari;
					    nodef=Language.enodef;
					    help=Language.ehelp;
					    yes=Language.eya;
					    no=Language.etidak;
					    topup=Language.etop; 
					    logout=Language.elogout;
					    Toast.makeText(getApplicationContext(), "English Language", Toast.LENGTH_SHORT).show();
						finish();
						
						}
						else if(lang)
						{
							SharedPreferences.Editor editor=MusikLegal.mPreferences.edit();
							editor.putString("Lang", "Indonesia");
							editor.commit();
							lang=false;
							jhentisubs=Language.jhentisubs;
							jmulaisubs=Language.jmulaisubs;
							jbangkrut=Language.jbangkrut;
							jteman=Language.jteman;
							joption=Language.joption;

							mulaisubs=Language.imulaisubs;
							mulaisubs2=Language.imulaisubs2;
						    hentisubs=Language.ihentisubs;
						    bangkrut=Language.ibangkrut;
						    gakonek=Language.igakonek;
						    error=Language.ierror;
						    thank=Language.ithanks;
						    sd=Language.isd;
						    expp=Language.iexpp;
						    expd=Language.iexpd;
						    del=Language.idel;
						    deal=Language.ideal;
						    playlist=Language.iplaylist;
						    info=Language.iinfo;
						    nosos=Language.inosos;
						    def=Language.idef;
						    cari=Language.icari;
						    nodef=Language.inodef;
						    help=Language.ihelp;
						    
						    yes=Language.iya;
						    no=Language.itidak;
						    topup=Language.itop;
						    logout=Language.ilogout;
						    Toast.makeText(getApplicationContext(), "Bahasa Indonesia", Toast.LENGTH_SHORT).show();
							finish();
						}
					}
					else
      				Toast.makeText(getApplicationContext(), Settings.gakonek, Toast.LENGTH_SHORT).show();
			
					
				}});
    }
    private InputSource retrieveInputStream(HttpEntity httpEntity) {
        InputSource insrc = null;
        try {
              insrc = new InputSource(httpEntity.getContent());
        } catch (Exception e) {
        }
        return insrc;
  } 
public boolean onCreateOptionsMenu(Menu menu){

		if(MusikLegal.mPreferences.getString("TWId", null)!=null)
		menu.add(0,MainMenu.Logout,0,"Disconnect Twitter");
		if(MusikLegal.mPreferences.getString("FBId", null)!=null)
		menu.add(0,MainMenu.DiscFB,0,"Disconnect FB");
	
		menu.add(0,MainMenu.TopUp,0,"SMS Subscribe");
		
		menu.add(0,MainMenu.Subs,0,MainMenu.subsc);
		

		return true;

		}

public boolean onOptionsItemSelected (MenuItem item){

	switch (item.getItemId()){

	
	case MainMenu.TopUp :
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		   alert.setTitle("Topup by Hard Voucher");
		   alert.setMessage("Masukkan 16 digit kode voucher");
	 
		   final EditText input = new EditText(this);
		   alert.setView(input);
		   alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			   public void onClick(DialogInterface dialog, int whichButton) {
			     
				   	String vouch = input.getText().toString();
				   	DefaultHttpClient client = new DefaultHttpClient();
			        HttpPost httppost = new HttpPost("http://topup.musiklegal.com/webservice.php");
			        List< BasicNameValuePair > nvps = new ArrayList< BasicNameValuePair >();
			        nvps.add(new BasicNameValuePair("username", MusikLegal.mPreferences.getString("UserName", "default")));
			        nvps.add(new BasicNameValuePair("cid", tid));
			        nvps.add(new BasicNameValuePair("voucher", vouch));
			 
			        try {   
			      	  	UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
			            
			            httppost.setEntity(p_entity);
			            
			            HttpResponse response = client.execute(httppost);
			            HttpEntity responseEntity = response.getEntity();
			            

			            SAXParserFactory spf = SAXParserFactory.newInstance();
			            SAXParser sp = spf.newSAXParser();
			            XMLReader xr = sp.getXMLReader();
			            
			            LoginHandler myLoginHandler = new LoginHandler();
			            xr.setContentHandler(myLoginHandler);
			            xr.parse(retrieveInputStream(responseEntity));
			            ParsedLoginDataSet parsedLoginDataSet = myLoginHandler.getParsedLoginData();
			           
			            
			            if (parsedLoginDataSet.getExtractedString().equals("SUCCESS")) 
			            {
			            	Toast.makeText(getApplicationContext(), "Saldo Musiklegal anda berhasil di topup", Toast.LENGTH_SHORT).show();
			            	
			            } else  
			            {
			            	Toast.makeText(getApplicationContext(), "Invalid voucher code", Toast.LENGTH_SHORT).show();
			            }
			      } catch (Exception e)
			      {
			          
			      }
			     }
			   });

			   alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			     public void onClick(DialogInterface dialog, int whichButton) {
			      
			     }
			   });
			   alert.setIcon(R.drawable.oc2);
			   alert.show(); 

	return true;
	
	case MainMenu.Subs:
		if(MusikLegal.internet(getApplicationContext())){
		if(MainMenu.subsc.equals("Stop Subscription")||MainMenu.subsc.equals("Berhenti berlangganan"))
		{	
			AlertDialog.Builder dialog2 = new AlertDialog.Builder(this);
        	dialog2.setTitle(MainMenu.subsc);
        	dialog2.setMessage(Settings.hentisubs);
        	dialog2.setCancelable(false);
        	dialog2.setPositiveButton(Settings.yes, new DialogInterface.OnClickListener() {
        	public void onClick(DialogInterface dialog, int id) 
        	{	           
        	   String username = MusikLegal.mPreferences.getString("UserName", "default");
				   String uid = MusikLegal.getUID(getApplicationContext());
			       String pass = MusikLegal.mPreferences.getString("PassWord", "default");
			       String subid = MusikLegal.mPreferences.getString("SubId", "default") ;
			    	
				   DefaultHttpClient client = new DefaultHttpClient();
			       HttpPost httppost = new HttpPost("http://secure.musiklegal.com/subscription/stop");
			       List< BasicNameValuePair > nvps = new ArrayList< BasicNameValuePair >();
			       nvps.add(new BasicNameValuePair("USERNAME", username));
			       nvps.add(new BasicNameValuePair("UID", uid));
			       nvps.add(new BasicNameValuePair("PASSWORD", pass));
			       nvps.add(new BasicNameValuePair("SUBSCRIPTION_ID", subid));

			       try {   
			    	    UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
    	            
    	            httppost.setEntity(p_entity);
    	            
    	            HttpResponse response = client.execute(httppost);
    	            HttpEntity responseEntity = response.getEntity();
    	            

    	            SAXParserFactory spf = SAXParserFactory.newInstance();
    	            SAXParser sp = spf.newSAXParser();
    	            XMLReader xr = sp.getXMLReader();
    	            
    	            LoginHandler myLoginHandler = new LoginHandler();
    	            xr.setContentHandler(myLoginHandler);
    	            xr.parse(retrieveInputStream(responseEntity));
    	            ParsedLoginDataSet parsedLoginDataSet = myLoginHandler.getParsedLoginData();
    	           
    	            
			           if (parsedLoginDataSet.getExtractedString().equals("SUCCESS")) 
			           {
			        	   MusikLegal.on =false;
			              MusikLegal.mPreferences.edit().remove("START").commit();
			              MusikLegal.mPreferences.edit().remove("SubId").commit();
			              MusikLegal.mPreferences.edit().remove("SubDate").commit();
			              MusikLegal.mPreferences.edit().commit();
			              MusikLegal.end_subscription = null;
			              if(MyD.currentSong!=null&&MyD.isInited)
	 			    		 {
	 			    		 MyD.currentSong.isplaying=false;
	 			    		 MyD.player.reset();
	 			    		 MyD.isInited=false;
	 			    		 MyD.paused=false;
	 			    		 MyD.currentSong=null;
	 			    		 }
			             // MainMenu.setStatus(Settings.this);
			            
			              MainMenu.setStatus(Settings.this);
			              MainMenu.subsc=Settings.jmulaisubs;
			        	  Toast.makeText(getApplicationContext(), "Stop Subscription Succes", Toast.LENGTH_SHORT).show();
			           } 
			           else 
			           {
			        	  Toast.makeText(getApplicationContext(), "Error occured", Toast.LENGTH_SHORT).show();
			                  
			           }
			           
			     } catch (Exception e)
			     {
			    	Toast.makeText(getApplicationContext(), "Error occured", Toast.LENGTH_SHORT).show();
					
			     }
        	}
        	});
        	dialog2.setNegativeButton(Settings.no, new DialogInterface.OnClickListener() { 
        		public void onClick(DialogInterface dialog, int which) {
        			dialog.dismiss();
        	} });
        	dialog2.create();
        	dialog2.setIcon(R.drawable.oc2);
            dialog2.show(); 
	
		}
		else if ((MusikLegal.mPreferences.getString("Balance", "0").equals("0"))){
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        	dialog.setTitle(Settings.jbangkrut);
        	dialog.setMessage(Settings.bangkrut);
        	dialog.setCancelable(false);
        	dialog.setPositiveButton("Input Voucher", new DialogInterface.OnClickListener() {
        	public void onClick(DialogInterface dialog, int id)
        	{
        		startActivity(new Intent(getApplicationContext(), Voucher.class));
        	}
        	});
        	dialog.setNegativeButton("SMS Subscribe", new DialogInterface.OnClickListener() { 
        		public void onClick(DialogInterface dialog, int which) {
        			startActivity(new Intent(getApplicationContext(), TopUp.class));
        	} });
        	dialog.create();
        	dialog.setIcon(R.drawable.oc2);
            dialog.show();
		}
		else if(MainMenu.subsc.equals("Start Subscription")||MainMenu.subsc.equals("Mulai berlangganan"))
    	{
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        	dialog.setTitle(Settings.jmulaisubs);
        	dialog.setMessage(Settings.mulaisubs+MusikLegal.mPreferences.getString("Balance", "0")+Settings.mulaisubs2);
        	dialog.setCancelable(false);
        	dialog.setPositiveButton(Settings.yes, new DialogInterface.OnClickListener() {
        	public void onClick(DialogInterface dialog, int id)
        	{
        		String username = MusikLegal.mPreferences.getString("UserName", "default");
    			String uid = MusikLegal.getUID(getApplicationContext());
    		    String pass = MusikLegal.mPreferences.getString("PassWord", "default");
    				
    			DefaultHttpClient client = new DefaultHttpClient();
    	        HttpPost httppost = new HttpPost("http://secure.musiklegal.com/subscription/start_weekly");
    	        List< BasicNameValuePair > nvps = new ArrayList< BasicNameValuePair >();
    	        nvps.add(new BasicNameValuePair("USERNAME", username));
    	        nvps.add(new BasicNameValuePair("UID", uid));
    	        nvps.add(new BasicNameValuePair("PASSWORD", pass));
    	 
    	        try {   
    	      	  	UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
    	            
    	            httppost.setEntity(p_entity);
    	            
    	            HttpResponse response = client.execute(httppost);
    	            HttpEntity responseEntity = response.getEntity();
    	            

    	            SAXParserFactory spf = SAXParserFactory.newInstance();
    	            SAXParser sp = spf.newSAXParser();
    	            XMLReader xr = sp.getXMLReader();
    	            
    	            LoginHandler myLoginHandler = new LoginHandler();
    	            xr.setContentHandler(myLoginHandler);
    	            xr.parse(retrieveInputStream(responseEntity));
    	            ParsedLoginDataSet parsedLoginDataSet = myLoginHandler.getParsedLoginData();
    	           
    	           

    	            SharedPreferences.Editor editor=MusikLegal.mPreferences.edit();
    	            if (parsedLoginDataSet.getExtractedString().equals("SUCCESS")) 
    	            {
    	            	
   	                  	editor.remove("SubDate").commit();
   	                  	editor.remove("START").commit();
   	                  	editor.remove("SubId").commit();
   	                  	MainMenu.endDate = parsedLoginDataSet.getDate();
   	                  	
    	            	if(parsedLoginDataSet.getSiD()!=null)
                       {
    	            		editor.putString("SubDate", parsedLoginDataSet.getDate()).commit();
	    	            	editor.putString("START", parsedLoginDataSet.getStart()).commit();
	                       	editor.putString("SubId", parsedLoginDataSet.getSiD()).commit();
	                       	
	                       	
                       }
    	            	MusikLegal.on = true;
    	            	MainMenu.subsc=Settings.jhentisubs;
    	            	MainMenu.setStatus(Settings.this);
    	            	
    	            	Toast.makeText(getApplicationContext(), Settings.thank, Toast.LENGTH_SHORT).show();
    	                 
    	            } else  
    	            {
    	            	Toast.makeText(getApplicationContext(), Settings.error, Toast.LENGTH_SHORT).show();
    	            }
    	      } catch (Exception e)
    	      {
    	          
    	      }
        	}
        	});
        	dialog.setNegativeButton(Settings.no, new DialogInterface.OnClickListener() { 
        		public void onClick(DialogInterface dialog, int which) {
        			dialog.dismiss();
        	} });
        	dialog.create();
        	dialog.setIcon(R.drawable.oc2);
            dialog.show();
		
    	}
		else
        {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Error");
		alertDialog.setMessage(Settings.gakonek);
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
			public void onClick(DialogInterface dialog, int which) {
			finish();
			}
		});
		alertDialog.setIcon(R.drawable.oc2);
		alertDialog.show();
        }
		
		}
	return true;
	
	case MainMenu.Help:
		if(MusikLegal.internet(getApplicationContext()))
		{
			LoginWeb.p = Settings.this;
			LoginWeb.urlTo="http://playerandroidhelp.musiklegal.com/";
			startActivity(new Intent(getApplicationContext(),LoginWeb.class));
			
		}
		else
			{Toast.makeText(getApplicationContext(), Settings.gakonek, Toast.LENGTH_SHORT).show();
			finish();
			}
	return true;
	
	case MainMenu.Logout:
		try{
		dcSocialNet("twitter");}
		catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	
	case MainMenu.DiscFB:
		try{
		dcSocialNet("facebook");}
		catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;	
	}
	return false;
}

void dcSocialNet(String net) throws ClientProtocolException, IOException, ParserConfigurationException, SAXException
{
	String address = "http://secure.musiklegal.com/socialnet/disconnect"; 
	HttpPost post = new HttpPost(address);
	
	List< BasicNameValuePair > nvps = new ArrayList< BasicNameValuePair >();
    
	nvps.add(new BasicNameValuePair("USERNAME", MusikLegal.mPreferences.getString("UserName", "NoUser")));
    nvps.add(new BasicNameValuePair("PASSWORD", MusikLegal.mPreferences.getString("PassWord", "NoUser")));
    nvps.add(new BasicNameValuePair("NETWORK", net));
    
    UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
    post.setEntity(p_entity);
    
	DefaultHttpClient dhc = new DefaultHttpClient();
	HttpResponse frlist = dhc.execute(post);
	new Thread(new Runnable(){

		public void run() {
			// TODO Auto-generated method stub
			
		}}).start();
	HttpEntity he = frlist.getEntity();
	SAXParserFactory spf = SAXParserFactory.newInstance();
	SAXParser sp = spf.newSAXParser();
	XMLReader xr = sp.getXMLReader();
	
	LoginHandler lh = new LoginHandler();
	xr.setContentHandler(lh);
	xr.parse(new InputSource(he.getContent()));
	if(lh.getParsedLoginData().getExtractedString().equals("SUCCESS"))
	{
		Toast.makeText(getApplicationContext(), "Success disconnecting "+net, Toast.LENGTH_SHORT).show();
		if(net=="twitter")
		{
		SharedPreferences.Editor editor=MusikLegal.mPreferences.edit();
		editor.remove("TWId");
		editor.commit();
		finish();
		}
		
		if(net=="facebook")
		{
			SharedPreferences.Editor editor=MusikLegal.mPreferences.edit();
			editor.remove("FBId");
			editor.commit();
		finish();
		}
	
	}
	else
	{
		Toast.makeText(getApplicationContext(), "Failed disconnecting "+net, Toast.LENGTH_SHORT).show();
	}
}

void check()
{
	String fb = MusikLegal.mPreferences.getString("FBId", null);
	String tw = MusikLegal.mPreferences.getString("TWId", null);
	if(fb==null)
	{
		fbcon.setVisibility(View.VISIBLE);
		this.fb.setVisibility(View.GONE);
	}
	else
	{
		fbcon.setVisibility(View.GONE);
		this.fb.setText("Facebook Name: "+fb);
		this.fb.setVisibility(View.VISIBLE);
	}
	if(tw==null)
	{
		twcon.setVisibility(View.VISIBLE);
		this.tw.setVisibility(View.GONE);
	}
	else
	{
		twcon.setVisibility(View.GONE);
		this.tw.setText("Twitter Name: "+tw);
		this.tw.setVisibility(View.VISIBLE);
	}
	
	if(fb==null&&tw==null)
	{
		MusikLegal.mPreferences.edit().remove("picture").commit();
	}
	
	 new Thread(new Runnable(){

			public void run() {
				h.post(new Runnable(){

					public void run() {
						Bitmap p = null ;
						String url = MusikLegal.mPreferences.getString("picture", null);
						if(url!=null)
							p= loadImage(url);
						if(p!=null)
							pp.setImageBitmap(p);
						else
							pp.setImageResource(R.drawable.header);
					}});
			}}).start();
}
		 Bitmap loadImage(String url)
		 {
			 File cache = new File(MusikLegal.albumArtFolder,"tempp");
				InputStream is = null;
				Bitmap bitmap;
				
					try {

						

						is = new URL(url).openStream();
						 OutputStream os = new FileOutputStream(cache);
				            Utils.CopyStream(is, os);
				            os.close();
				            bitmap = TrackInfo.decodeFile(cache);
				            is.close();
				            return bitmap;
						
					

					} 
					catch (MalformedURLException e) {
						Log.d("gambar","error "+e.getMessage() );
						e.printStackTrace();
						return null;
					} catch (IOException e) {
						
						Log.d("gambar","error "+e.getMessage() );
						e.printStackTrace();
						return null;
					}
					
					
				
		 }

}